//
//  MainBC.swift
//  我的微博
//
//  Created by shmily on 15/7/28.
//  Copyright © 2015年 Stanford-IOS8-Swift-shmilyAshen. All rights reserved.
//

import UIKit

///主页
class MainBC: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        addVCs()
    }
    
    //发送微博
    func btnNewBlog() {
        let nav = UINavigationController(rootViewController: SendBlogVC())
        
        presentViewController(nav, animated: true, completion: nil)
    }
    
    
    //MARK:- 布局控件
    ///懒加载
    lazy var button :UIButton = {
        let btnAdd = UIButton()
        let width = UIScreen.mainScreen().bounds.width / CGFloat(self.childViewControllers.count)
        var rect = CGRect(x: 0.0, y: 0.0, width:width , height: self.tabBar.bounds.height)
        rect.offset(dx: width * 2, dy: 0)
        btnAdd.frame = rect
        
        //设置图像
        btnAdd.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        btnAdd.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        btnAdd.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        btnAdd.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        btnAdd.addTarget(self, action: "btnNewBlog", forControlEvents: UIControlEvents.TouchUpInside)
        return btnAdd
    }()
    
    ///添加按钮
    override func viewWillLayoutSubviews() {
        tabBar.addSubview(button)
    }
    
    //添加控制器到 tabBar
    func addVCs() {
        
        addChildViewController(homeTableVC(), title: "主页", icon: "tabbar_home")
        addChildViewController(messageTableVC(), title: "消息", icon: "tabbar_message_center")
        
        addChildViewController(UIViewController())
        
        addChildViewController(DiscoverTableVC(), title: "发现", icon: "tabbar_discover")
        addChildViewController(profileTableVC(), title: "我", icon: "tabbar_profile")
    }
    
    ///添加子控制器
    func addChildViewController(vc: UIViewController,title:String,icon:String) {
        vc.title = title
        vc.tabBarItem.image = UIImage(named: icon)
        let nav = UINavigationController(rootViewController: vc)
        
        addChildViewController(nav)
    }

}
